package com.micangcaihang.micang.mvp.task.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.micangcaihang.common.base.fragment.BaseFragment;
import com.micangcaihang.common.utils.Properties;
import com.micangcaihang.common.utils.StringUtils;
import com.micangcaihang.common.utils.ToastUtils;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.databinding.FragmentTaskTuftMatingBinding;
import com.micangcaihang.micang.entity.PickerEntity;
import com.micangcaihang.micang.entity.TaskTuftMatingEntity;
import com.micangcaihang.micang.http.request.TaskTuftMatingRequest;
import com.micangcaihang.micang.mvp.task.contract.TaskTuftMatingContract;
import com.micangcaihang.micang.mvp.task.presenter.TaskTuftMatingPresenter;
import com.micangcaihang.micang.view.PickerViewHelper;

import java.util.List;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/24
 *     update : 2017/07/24
 *     desc   : 房屋配套
 *     version: 1.0
 * </pre>
 */

public class TaskTuftMatingFragment extends BaseFragment<TaskTuftMatingPresenter, FragmentTaskTuftMatingBinding> implements TaskTuftMatingContract.View {
    private String id;
    private TaskTuftMatingRequest mRequest;
    private TaskTuftMatingRequest mOldRequest;
    private int pickerType;
    private boolean isNetSuccess;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getArguments().getString("id");
    }

    public static TaskTuftMatingFragment newInstance(String id) {
        Bundle args = new Bundle();
        TaskTuftMatingFragment fragment = new TaskTuftMatingFragment();
        args.putString("id", id);
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 获取资源文件布局
     *
     * @return 资源布局文件layout
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_task_tuft_mating;
    }

    @Override
    protected void initData() {
        super.initData();
        mRequest = new TaskTuftMatingRequest();
        mOldRequest = new TaskTuftMatingRequest();
        mOldRequest.setId(id);
        mRequest.setId(id);
    }

    @Override
    public void initPresenter() {
        super.initPresenter();
        mPresenter.setId(id);
    }

    /**
     * 初始化按钮监听
     */
    @Override
    protected void initListener() {
        mViewBinding.setClickHandler(new ClickHandler());
    }

    /**
     * 返回下拉框信息
     *
     * @param list
     */
    @Override
    public void returnPickerViewData(List<PickerEntity> list, String type) {
        switch (pickerType) {
            case 1:
                PickerViewHelper.showOnePickerView(_mActivity, type, mRequest.getSupport_water(), list,
                        pickerEntity1 -> {
                            mRequest.setSupport_water(pickerEntity1.getSign_id());
                            mViewBinding.tvSupportWater.setText(pickerEntity1.getTitle());
                        });
                break;
            case 2:
                PickerViewHelper.showOnePickerView(_mActivity, type, mRequest.getSupport_gas(), list,
                        pickerEntity1 -> {
                            mRequest.setSupport_gas(pickerEntity1.getSign_id());
                            mViewBinding.tvSupportGas.setText(pickerEntity1.getTitle());
                        });
                break;
            case 3:
                PickerViewHelper.showOnePickerView(_mActivity, type, mRequest.getSupport_heat(), list,
                        pickerEntity1 -> {
                            mRequest.setSupport_heat(pickerEntity1.getSign_id());
                            mViewBinding.tvSupportHeat.setText(pickerEntity1.getTitle());
                        });
                break;
            case 4:
                PickerViewHelper.showOnePickerView(_mActivity, type, mRequest.getSupport_lift(), list,
                        pickerEntity1 -> {
                            mRequest.setSupport_lift(pickerEntity1.getSign_id());
                            mViewBinding.tvSupportLift.setText(pickerEntity1.getTitle());
                        });
                break;
            case 5:
                PickerViewHelper.showOnePickerView(_mActivity, type, mRequest.getSupport_carport(), list,
                        pickerEntity1 -> {
                            mRequest.setSupport_carport(pickerEntity1.getSign_id());
                            mViewBinding.tvSupportCarport.setText(pickerEntity1.getTitle());
                            if ("4".equals(mRequest.getSupport_carport())) {
                                mViewBinding.expandableLayoutSupportOtherCarport.expand();
                            } else {
                                mViewBinding.expandableLayoutSupportOtherCarport.collapse();
                            }
                        });
                break;
            case 6:
                PickerViewHelper.showOnePickerView(_mActivity, type, mRequest.getHas_service_charge(), list,
                        pickerEntity1 -> {
                            mRequest.setHas_service_charge(pickerEntity1.getSign_id());
                            mViewBinding.tvHasServiceCharge.setText(pickerEntity1.getTitle());
                            if ("1".equals(mRequest.getHas_service_charge())) {
                                mViewBinding.expandableLayoutHasServiceCharge.expand();
                            } else {
                                mViewBinding.expandableLayoutHasServiceCharge.collapse();
                            }
                        });

                break;
            default:
                break;
        }
    }

    /**
     * 返回房屋配套信息
     *
     * @param entity
     */
    @Override
    public void returnTuftMatingData(TaskTuftMatingEntity entity) {
        isNetSuccess = true;
        String[] excludsArray = new String[]{"choose_list", "id"};
        try {
            Properties.copyPropertiesExclude(entity, mRequest, excludsArray);
            Properties.copyPropertiesExclude(entity, mOldRequest, excludsArray);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //其他车位
        if (!StringUtils.isEmpty(entity.getSupport_other_carport())) {
            mViewBinding.etSupportOtherCarport.setText(entity.getSupport_other_carport());
        }
        //物业名称
        if (!StringUtils.isEmpty(entity.getService_name())) {
            mViewBinding.etServiceName.setText(entity.getService_name());
        }
        //物业价格
        if (!StringUtils.isEmpty(entity.getService_money_standard())) {
            mViewBinding.etServiceMoneyStandard.setText(entity.getService_money_standard());
        }
        if ("4".equals(mRequest.getSupport_carport())) {
            mViewBinding.expandableLayoutSupportOtherCarport.expand();
        } else {
            mViewBinding.expandableLayoutSupportOtherCarport.collapse();
        }
        if ("1".equals(mRequest.getHas_service_charge())) {
            mViewBinding.expandableLayoutHasServiceCharge.expand();
        } else {
            mViewBinding.expandableLayoutHasServiceCharge.collapse();
        }
        TaskTuftMatingEntity.ChooseListBean chooseList = entity.getChoose_list();
        if (chooseList != null) {
            //供水
            if (!StringUtils.isEmpty(chooseList.getSupport_water())) {
                mViewBinding.tvSupportWater.setText(chooseList.getSupport_water());
            }
            //燃气
            if (!StringUtils.isEmpty(chooseList.getSupport_gas())) {
                mViewBinding.tvSupportGas.setText(chooseList.getSupport_gas());
            }
            //供暖
            if (!StringUtils.isEmpty(chooseList.getSupport_heat())) {
                mViewBinding.tvSupportHeat.setText(chooseList.getSupport_heat());
            }
            //电梯
            if (!StringUtils.isEmpty(chooseList.getSupport_lift())) {
                mViewBinding.tvSupportLift.setText(chooseList.getSupport_lift());
            }
            //车位
            if (!StringUtils.isEmpty(chooseList.getSupport_carport())) {
                mViewBinding.tvSupportCarport.setText(chooseList.getSupport_carport());
            }
            //物业服务
            if (!StringUtils.isEmpty(chooseList.getHas_service_charge())) {
                mViewBinding.tvHasServiceCharge.setText(chooseList.getHas_service_charge());
            }
            if ("4".equals(mRequest.getSupport_carport())) {
                mRequest.setSupport_other_carport(entity.getSupport_other_carport());
                mOldRequest.setSupport_other_carport(entity.getSupport_other_carport());
            } else {
                mRequest.setSupport_other_carport("");
                mOldRequest.setSupport_other_carport("");
            }
            if ("1".equals(mRequest.getHas_service_charge())) {
                mRequest.setService_name(entity.getService_name());
                mRequest.setService_money_standard(entity.getService_money_standard());
                mOldRequest.setService_name(entity.getService_name());
                mOldRequest.setService_money_standard(entity.getService_money_standard());
            } else {
                mRequest.setService_name("");
                mRequest.setService_money_standard("");
                mOldRequest.setService_name("");
                mOldRequest.setService_money_standard("");
            }
        }
    }

    public class ClickHandler {
        public void onShowSupportWaterPickerView(View view) {
            pickerType = 1;
            mPresenter.loadPickerViewData("support_water", "供水");
        }

        public void onShowSupportGasPickerView(View view) {
            pickerType = 2;
            mPresenter.loadPickerViewData("support_gas", "燃气");
        }

        public void onShowSupportHeatPickerView(View view) {
            pickerType = 3;
            mPresenter.loadPickerViewData("support_heat", "供暖");

        }

        public void onShowSupportLiftPickerView(View view) {
            pickerType = 4;
            mPresenter.loadPickerViewData("support_lift", "电梯");

        }

        public void onShowSupportCarportPickerView(View view) {
            pickerType = 5;
            mPresenter.loadPickerViewData("support_carport", "车位");

        }

        public void onShowHasServiceChargePickerView(View view) {
            pickerType = 6;
            mPresenter.loadPickerViewData("is_have", "物业服务");

        }

        public void onSave(View view) {
            saveData();
        }
    }

    /**
     * 开始保存数据请求
     */
    public void saveData() {
        setSaveData();
        if (mRequest.toString().equals(mOldRequest.toString())) {
            ToastUtils.showLong("您没有更改任何信息，无需保存");
            return;
        }
        isNetSuccess = false;
        mPresenter.saveTuftMaingData(mRequest);
    }

    public boolean isSaveData() {
        //如果请求错误则返回true
        if (!isNetSuccess) {
            return true;
        }
        setSaveData();
        if (mRequest.toString().equals(mOldRequest.toString())) {
            return true;
        } else {
            return false;
        }
    }

    private void setSaveData() {
        if ("4".equals(mRequest.getSupport_carport())) {
            mRequest.setSupport_other_carport(mViewBinding.etSupportOtherCarport.getText().toString().trim());
        } else {
            mRequest.setSupport_other_carport("");
        }
        if ("1".equals(mRequest.getHas_service_charge())) {
            mRequest.setService_name(mViewBinding.etServiceName.getText().toString().trim());
            mRequest.setService_money_standard(mViewBinding.etServiceMoneyStandard.getText().toString().trim());
        } else {
            mRequest.setService_name("");
            mRequest.setService_money_standard("");
        }
    }
}
